package com.oa.framework.org.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.common.exception.CreateObjectException;
import com.oa.common.exception.UpdateObjectException;
import com.oa.common.identity.IdGenerateUtils;
import com.oa.framework.cache.DutyCache;
import com.oa.framework.org.dao.DutyDao;
import com.oa.framework.org.domain.Duty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cuichao
 */
@Service
public class DutyServiceImpl implements DutyService {

    @Autowired
    private DutyDao dutyDao;

    @Autowired
    private DutyCache dutyCache;


    @Override
    public List<Duty> loadDuties() {
        List<Duty> duties =dutyCache.get();
        if(duties != null){
            return duties;
        }
        duties = dutyDao.selectList(null);
        if(CollectionUtils.isEmpty(duties)){
            return duties;
        }
        dutyCache.put(duties);
        return duties;
    }

    @Override
    public List<Duty> loadEnableDuties() {
        List<Duty> duties = loadDuties();
        if(CollectionUtils.isEmpty(duties)){
            return duties;
        }
        return duties.stream().filter(v -> !v.getDisable()).collect(Collectors.toList());
    }


    @Override
    public IPage<Duty> search(IPage<Duty> page) {
        return dutyDao.search(page);
    }

    @Override
    public Duty getDutyDetail(Long dutyId) {
        return dutyDao.getDutyDetail(dutyId);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addDuty(Duty duty) throws CreateObjectException {
        final int createSuccessFlag = 1;
        int flag = dutyDao.insert(duty);
        if (createSuccessFlag != flag) {
            throw new CreateObjectException();
        }
        duty.getPermissions().forEach(v -> v.setPermissionId(Long.valueOf(IdGenerateUtils.snowflake())));
        flag = dutyDao.addPermissions(duty.getDutyId(), duty.getPermissions());
        if (flag <= 0) {
            throw new CreateObjectException();
        }
        dutyCache.remove();
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateDuty(Duty duty) throws UpdateObjectException {
        final int updateSuccessFlag = 1;
        int flag = dutyDao.updateById(duty);
        if (updateSuccessFlag != flag) {
            throw new UpdateObjectException();
        }
        dutyDao.removePermissions(duty.getDutyId());
        duty.getPermissions().forEach(v -> v.setPermissionId(Long.valueOf(IdGenerateUtils.snowflake())));
        flag = dutyDao.addPermissions(duty.getDutyId(), duty.getPermissions());
        if (flag <= 0) {
            throw new UpdateObjectException();
        }
        dutyCache.remove();
        return true;
    }

    @Override
    public boolean disable(Long dutyId) {
        final int successFlag = 1;
        int flag = dutyDao.disableDuty(dutyId);
        if (successFlag == flag) {
            return true;
        }
        return false;
    }
}
